﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;


namespace SPBusiness
{
   public sealed class Field
   {
      /// <summary>
      /// Field class has only Static Methods, no constructor needed.
      /// </summary>
      private Field() { }

      public static object GetLatestVersionedValue(SPListItem vSPListItem, string vFieldStaticName)
      {
         try
         {

            if (vSPListItem[vFieldStaticName] != null)
            {
               return vSPListItem[vFieldStaticName];
            }
            else
            {
               if (vSPListItem.ID > 0)
               {
                  if (vSPListItem.Versions.Count > 0)
                  {
                     for (int Count = 0; Count < vSPListItem.Versions.Count; Count++)
                     {
                        if (vSPListItem.Versions[Count][vFieldStaticName] != null)
                        {
                           return vSPListItem.Versions[Count][vFieldStaticName];

                        }
                     }
                  }
               }
            }

            return null;
         }
         catch (Exception ex)
         {
            Logging.LogMessase(typeof(Field).Namespace, typeof(Field).Name, "GetLatestVersionedValue", ex);
            throw;
         }
      }


      /// <summary>
      /// Get the value of a SPField from a SPListItem
      /// MSOCAF: THE FIELD IS CAST ONLY ONCE, DEPENDING ON THE TypeAsString Property.
      /// </summary>
      /// <param name="pSPListItem"></param>
      /// <param name="pSPField"></param>
      /// <returns></returns>
      public static object GetValue(SPListItem pSPListItem, SPField pSPField)
      {
         try
         {

            object vReturn = null;

            switch (pSPField.TypeAsString)
            {
               case "Boolean":
                  vReturn = GetValueString(pSPListItem, pSPField);
                  break;
               case "Counter":
                  vReturn = GetValueString(pSPListItem, pSPField);
                  break;
               case "DateTime":
                  vReturn = GetValueDateTime(pSPListItem, pSPField);
                  break;
               case "Note":
                  vReturn = GetValueString(pSPListItem, pSPField);
                  break;
               case "Number":
                  vReturn = GetValueString(pSPListItem, pSPField);
                  break;
               case "Text":
                  vReturn = GetValueString(pSPListItem, pSPField);
                  break;
               case "User":

                  SPFieldUser vSPFieldUser = pSPField as SPFieldUser;
                  if (!vSPFieldUser.AllowMultipleValues)
                  {
                     SPFieldUserValue vSPFieldUserValue = GetSPFieldUserValue(pSPListItem.Web.Site.Url, pSPListItem.Web.Url, pSPListItem[pSPField.Id]);
                     vReturn = GetValueString(vSPFieldUserValue);
                  }
                  else
                  {
                     SPFieldUserValueCollection vSPFieldUserValueCollection = GetSPFieldUserValueCollection(pSPListItem.Web.Site.Url, pSPListItem.Web.Url, pSPListItem[pSPField.Id]);
                     vReturn = GetValueString(vSPFieldUserValueCollection);
                  }
                  break;
               case "Lookup":
                  SPFieldLookup vSPFieldLookup = pSPField as SPFieldLookup;
                  if (!vSPFieldLookup.AllowMultipleValues)
                  {
                     SPFieldLookupValue vSPFieldLookupValue = GetSPFieldLookupValue(pSPListItem[pSPField.Id]);
                     vReturn = GetValueString(vSPFieldLookupValue);
                  }
                  else
                  {
                     SPFieldLookupValueCollection vSPFieldLookupValueCollection = GetSPFieldLookupValueCollection(pSPListItem[pSPField.Id]);
                     vReturn = GetValueString(vSPFieldLookupValueCollection);
                  }
                  break;
               case "FilteredLookupField":
                  SPFieldLookup vFilteredLookupField = pSPField as SPFieldLookup;
                  if (!vFilteredLookupField.AllowMultipleValues)
                  {
                     SPFieldLookupValue vFilteredLookupFieldValue = GetSPFieldLookupValue(pSPListItem[pSPField.Id]);
                     vReturn = GetValueString(vFilteredLookupFieldValue);
                  }
                  else
                  {
                     SPFieldLookupValueCollection vFilteredLookupValueCollection = GetSPFieldLookupValueCollection(pSPListItem[pSPField.Id]);
                     vReturn = GetValueString(vFilteredLookupValueCollection);
                  }
                  break;
               case "URL":
                  vReturn = GetValueString(GetSPFieldUrlValue(pSPListItem[pSPField.Id]));
                  break;
            }

            return vReturn;

         }
         catch (Exception ex)
         {
            Logging.LogMessase(typeof(Field).Namespace, typeof(Field).Name, "GetValue", ex);
            throw;
         }
      }

      public static string GetValueDateTime(SPListItem pSPListItem, SPField pSPField)
      {
         try
         {
            if (pSPListItem[pSPField.Id] != null)
            {
               DateTime vDateTime1 = (DateTime)pSPListItem[pSPField.Id];

               DateTime vDateTime2 = pSPListItem.Web.RegionalSettings.TimeZone.LocalTimeToUTC(vDateTime1);

               return pSPListItem.Fields[pSPField.Id].GetFieldValueAsHtml(vDateTime2);
            }
            else
            {
               return string.Empty;
            }

         }
         catch (Exception ex)
         {
            Logging.LogMessase(typeof(Field).Namespace, typeof(Field).Name, "GetValueDateTime", ex);
            throw;
         }
      }

      public static string GetValueString(SPListItem pSPListItem, SPField pSPField)
      {
         try
         {
            if (pSPListItem[pSPField.Id] != null)
            {
               return pSPListItem[pSPField.Id].ToString();
            }
            else
            {
               return string.Empty;
            }

         }
         catch (Exception ex)
         {
            Logging.LogMessase(typeof(Field).Namespace, typeof(Field).Name, "GetValueString", ex);
            throw;
         }
      }

      public static string GetValueString(SPFieldUserValue pSPFieldUserValue)
      {
         try
         {
            if (pSPFieldUserValue != null)
            {
               return pSPFieldUserValue.LookupValue;
            }
            else
            {
               return string.Empty;
            }


         }
         catch (Exception ex)
         {
            Logging.LogMessase(typeof(Field).Namespace, typeof(Field).Name, "GetValueString", ex);
            throw;
         }
      }

      public static string GetValueString(SPFieldUserValueCollection pSPFieldUserValue)
      {
         try
         {
            if (pSPFieldUserValue != null)
            {
               string[] vCollection = pSPFieldUserValue.OfType<SPFieldUserValue>().Select(x => x.LookupValue).ToArray();

               StringBuilder vComplexText = null;

               vComplexText = new StringBuilder();

               vComplexText.AppendLine("<ul>");

               foreach (string vValue in vCollection)
               {
                  vComplexText.AppendLine("<li>" + vValue + "</li>");
               }

               vComplexText.AppendLine("</ul>");

               return vComplexText.ToString();
            }
            else
            {
               return string.Empty;
            }


         }
         catch (Exception ex)
         {
            Logging.LogMessase(typeof(Field).Namespace, typeof(Field).Name, "GetValueString", ex);
            throw;
         }
      }

      public static string GetValueString(SPFieldLookupValue pSPFieldLookupValue)
      {
         try
         {
            if (pSPFieldLookupValue != null)
            {
               return pSPFieldLookupValue.LookupValue;
            }
            else
            {
               return string.Empty;
            }


         }
         catch (Exception ex)
         {
            Logging.LogMessase(typeof(Field).Namespace, typeof(Field).Name, "GetValueString", ex);
            throw;
         }
      }

      public static string GetValueString(SPFieldLookupValueCollection pSPFieldLookupValueCollection)
      {
         try
         {
            if (pSPFieldLookupValueCollection != null)
            {
               string[] vCollection = pSPFieldLookupValueCollection.OfType<SPFieldLookupValue>().Select(x => x.LookupValue).ToArray();

               StringBuilder vComplexText = null;

               vComplexText = new StringBuilder();

               vComplexText.AppendLine("<ul>");

               foreach (string vValue in vCollection)
               {
                  vComplexText.AppendLine("<li>" + vValue + "</li>");
               }

               vComplexText.AppendLine("</ul>");

               return vComplexText.ToString();

            }
            else
            {
               return string.Empty;
            }


         }
         catch (Exception ex)
         {
            Logging.LogMessase(typeof(Field).Namespace, typeof(Field).Name, "GetValueString", ex);
            throw;
         }
      }

      public static string GetValueString(SPFieldUrlValue pSPFieldUrlValue)
      {
         try
         {
            if (pSPFieldUrlValue != null)
            {
               return pSPFieldUrlValue.Url;
            }
            else
            {
               return string.Empty;
            }


         }
         catch (Exception ex)
         {
            Logging.LogMessase(typeof(Field).Namespace, typeof(Field).Name, "GetValueString", ex);
            throw;
         }
      }

      public static SPFieldLookupValue GetSPFieldLookupValue(object pobject)
      {
         try
         {
            if (pobject != null)
            {
               return new SPFieldLookupValue(pobject.ToString());
            }
            else
            {
               return null;
            }


         }
         catch (Exception ex)
         {
            Logging.LogMessase(typeof(Field).Namespace, typeof(Field).Name, "GetSPFieldLookupValue", ex);
            throw;
         }
      }

      public static SPFieldLookupValueCollection GetSPFieldLookupValueCollection(object pobject)
      {
         try
         {
            if (pobject != null)
            {
               return new SPFieldLookupValueCollection(pobject.ToString());
            }
            else
            {
               return null;
            }


         }
         catch (Exception ex)
         {
            Logging.LogMessase(typeof(Field).Namespace, typeof(Field).Name, "GetSPFieldLookupValueCollection", ex);
            throw;
         }
      }

      public static SPFieldUserValue GetSPFieldUserValue(string pSiteUrl, string pWebUrl, object pobject)
      {
         try
         {
            if (pobject != null)
            {
               using (SPSite vSPSite = new SPSite(pSiteUrl))
               {
                  using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                  {
                     return new SPFieldUserValue(vSPWeb, pobject.ToString());
                  }
               }
            }
            else
            {
               return null;
            }
         }
         catch (Exception ex)
         {
            Logging.LogMessase(typeof(Field).Namespace, typeof(Field).Name, "GetSPFieldUserValue", ex);
            throw;
         }
      }

      public static SPFieldUserValueCollection GetSPFieldUserValueCollection(string pSiteUrl, string pWebUrl, object pobject)
      {
         try
         {
            if (pobject != null)
            {
               using (SPSite vSPSite = new SPSite(pSiteUrl))
               {
                  using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                  {
                     return new SPFieldUserValueCollection(vSPWeb, pobject.ToString());

                  }
               }
            }
            else
            {
               return null;
            }


         }
         catch (Exception ex)
         {
            Logging.LogMessase(typeof(Field).Namespace, typeof(Field).Name, "GetSPFieldUserValueCollection", ex);
            throw;
         }
      }

      public static SPFieldUrlValue GetSPFieldUrlValue(object pobject)
      {
         try
         {
            if (pobject != null)
            {
               return new SPFieldUrlValue(pobject.ToString());
            }
            else
            {
               return null;
            }


         }
         catch (Exception ex)
         {
            Logging.LogMessase(typeof(Field).Namespace, typeof(Field).Name, "GetSPFieldUrlValue", ex);
            throw;
         }
      }

   }
}
